<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.2/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"littlefxc.github.io","root":"/blog/","images":"/blog/images","scheme":"Mist","version":"8.2.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/blog/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>
<meta name="description" content="[TOC] 1 理论1.1 内存结构  线程共享：堆，方法区 线程隔离：虚拟机栈，本地方法栈，程序计数器">
<meta property="og:type" content="article">
<meta property="og:title" content="JVM 调优">
<meta property="og:url" content="http://littlefxc.github.io/2021/02/23/JVM-%E8%B0%83%E4%BC%98/index.html">
<meta property="og:site_name" content="一年春又来">
<meta property="og:description" content="[TOC] 1 理论1.1 内存结构  线程共享：堆，方法区 线程隔离：虚拟机栈，本地方法栈，程序计数器">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/JVM%E5%86%85%E5%AD%98%E7%BB%93%E6%9E%84.png">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/JVM%E5%86%85%E5%AD%98%E7%BB%93%E6%9E%84-%E5%A0%86.png">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/JVM%E5%86%85%E5%AD%98%E7%BB%93%E6%9E%84-%E8%99%9A%E6%8B%9F%E6%9C%BA%E6%A0%88.png">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/JVM%E5%86%85%E5%AD%98%E7%BB%93%E6%9E%84-%E6%96%B9%E6%B3%95%E5%8C%BA.png">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/image-20210223220317252.png">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/%E7%B1%BB%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B%E8%AF%A6%E8%A7%A3.png">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/image-20210224212914755.png">
<meta property="article:published_time" content="2021-02-23T12:49:45.000Z">
<meta property="article:modified_time" content="2021-04-25T07:58:55.797Z">
<meta property="article:author" content="一年春又来">
<meta property="article:tag" content="JVM">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/littlefxc/oss/raw/master/images/JVM%E5%86%85%E5%AD%98%E7%BB%93%E6%9E%84.png">


<link rel="canonical" href="http://littlefxc.github.io/2021/02/23/JVM-%E8%B0%83%E4%BC%98/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>
<title>JVM 调优 | 一年春又来</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<link rel="alternate" href="/blog/atom.xml" title="一年春又来" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/blog/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">一年春又来</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/blog/" rel="section"><i class="home                          //首页 fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-archives"><a href="/blog/archives/" rel="section"><i class="archive          //归档 fa-fw"></i>归档</a></li>
        <li class="menu-item menu-item-categories"><a href="/blog/categories/" rel="section"><i class="th           //分类 fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-tags"><a href="/blog/tags/" rel="section"><i class="tags                     //标签 fa-fw"></i>标签</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#1-%E7%90%86%E8%AE%BA"><span class="nav-text">1 理论</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-1-%E5%86%85%E5%AD%98%E7%BB%93%E6%9E%84"><span class="nav-text">1.1 内存结构</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-1-%E5%A0%86"><span class="nav-text">1.1.1 堆</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-2-%E8%99%9A%E6%8B%9F%E6%9C%BA%E6%A0%88"><span class="nav-text">1.1.2 虚拟机栈</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-2-%E6%9C%AC%E5%9C%B0%E6%96%B9%E6%B3%95%E6%A0%88"><span class="nav-text">1.1.2 本地方法栈</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-4-%E7%A8%8B%E5%BA%8F%E8%AE%A1%E6%95%B0%E5%99%A8"><span class="nav-text">1.1.4 程序计数器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-5-%E6%96%B9%E6%B3%95%E5%8C%BA"><span class="nav-text">1.1.5 方法区</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-2-%E7%B1%BB%E5%8A%A0%E8%BD%BD%E6%9C%BA%E5%88%B6"><span class="nav-text">1.2 类加载机制</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-1-%E7%BC%96%E8%AF%91"><span class="nav-text">1.2.1 编译</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-2-%E5%8A%A0%E8%BD%BD"><span class="nav-text">1.2.2 加载</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-3-%E9%93%BE%E6%8E%A5"><span class="nav-text">1.2.3 链接</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-3-1-%E9%AA%8C%E8%AF%81"><span class="nav-text">1.2.3.1 验证</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-3-2-%E5%87%86%E5%A4%87"><span class="nav-text">1.2.3.2 准备</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-3-3-%E8%A7%A3%E6%9E%90"><span class="nav-text">1.2.3.3 解析</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-4-%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="nav-text">1.2.4 初始化</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-5-%E4%BD%BF%E7%94%A8"><span class="nav-text">1.2.5 使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-6-%E5%8D%B8%E8%BD%BD"><span class="nav-text">1.2.6 卸载</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-7-%E5%B0%8F%E8%8A%82"><span class="nav-text">1.2.7 小节</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-3-%E7%BC%96%E8%AF%91%E5%99%A8%E4%BC%98%E5%8C%96"><span class="nav-text">1.3 编译器优化</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-4-%E5%9E%83%E5%9C%BE%E6%94%B6%E9%9B%86%E7%AE%97%E6%B3%95"><span class="nav-text">1.4 垃圾收集算法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-5-%E5%9E%83%E5%9C%BE%E6%94%B6%E9%9B%86%E5%99%A8"><span class="nav-text">1.5 垃圾收集器</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#2-%E5%B7%A5%E5%85%B7"><span class="nav-text">2 工具</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#3-%E5%AE%9E%E6%88%98"><span class="nav-text">3 实战</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#4-%E5%8F%82%E8%80%83%E8%B5%84%E6%BA%90"><span class="nav-text">4 参考资源</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">一年春又来</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">184</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">35</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">115</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/02/23/JVM-%E8%B0%83%E4%BC%98/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          JVM 调优
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-02-23 20:49:45" itemprop="dateCreated datePublished" datetime="2021-02-23T20:49:45+08:00">2021-02-23</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-04-25 15:58:55" itemprop="dateModified" datetime="2021-04-25T15:58:55+08:00">2021-04-25</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <p>[TOC]</p>
<h1 id="1-理论"><a href="#1-理论" class="headerlink" title="1 理论"></a>1 理论</h1><h2 id="1-1-内存结构"><a href="#1-1-内存结构" class="headerlink" title="1.1 内存结构"></a>1.1 内存结构</h2><p><img src="https://gitee.com/littlefxc/oss/raw/master/images/JVM%E5%86%85%E5%AD%98%E7%BB%93%E6%9E%84.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/JVM内存结构.png"></p>
<ul>
<li>线程共享：堆，方法区</li>
<li>线程隔离：虚拟机栈，本地方法栈，程序计数器</li>
</ul>
<span id="more"></span>

<h3 id="1-1-1-堆"><a href="#1-1-1-堆" class="headerlink" title="1.1.1 堆"></a>1.1.1 堆</h3><p>堆又做了细分如下图所示：</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/JVM%E5%86%85%E5%AD%98%E7%BB%93%E6%9E%84-%E5%A0%86.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/JVM内存结构-堆.png"></p>
<p>JDK8 之前堆分为新生代、老年代和持久代（也叫永久代），其中新生代中又有伊甸园和存活区，而存活区又分为 “From survivor” 和 “To survivor”。</p>
<p>JDK8 之后，持久代被废弃，由元空间代替，而元空间并不是堆内存的一部分，元空间是本地内存。</p>
<h3 id="1-1-2-虚拟机栈"><a href="#1-1-2-虚拟机栈" class="headerlink" title="1.1.2 虚拟机栈"></a>1.1.2 虚拟机栈</h3><p>虚拟机栈是线程独享的，当创建一个现成的时候就会创建虚拟机栈。</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/JVM%E5%86%85%E5%AD%98%E7%BB%93%E6%9E%84-%E8%99%9A%E6%8B%9F%E6%9C%BA%E6%A0%88.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/JVM内存结构-虚拟机栈.png"></p>
<ul>
<li>虚拟机栈由栈帧组成。</li>
<li>每一次方法调用都会创建一个栈帧，然后去压栈。</li>
<li>方法返回的时候代表栈帧的出栈操作。</li>
<li>栈帧里面包含一系列数据：局部变量表、操作数栈、指向运行时常量池的引用、方法返回地址和动态链接</li>
</ul>
<h3 id="1-1-2-本地方法栈"><a href="#1-1-2-本地方法栈" class="headerlink" title="1.1.2 本地方法栈"></a>1.1.2 本地方法栈</h3><p>虚拟机栈中放的是Java 方法，而本地方法栈放的是 native 方法（如 UnSafe类）。</p>
<h3 id="1-1-4-程序计数器"><a href="#1-1-4-程序计数器" class="headerlink" title="1.1.4 程序计数器"></a>1.1.4 程序计数器</h3><p>程序计数器用来记录各个字节码执行的字节码的地址，像分支、循环、跳转、异常、线程恢复等等操作都需依赖程序计数器。</p>
<ul>
<li><p><strong>为什么需要程序计数器?</strong></p>
<p>这是因为Java 是个多线程语言，当执行的线程数量超过CPU核心的时候，线程之间就会根据时间片争抢CPU资源。例如，某个线程它的任务还没有执行完成，CPU 就被其它线程抢走，如果之后又轮到这个线程执行任务，那么就得知道从哪里继续执行任务，所以会为每一个线程分配一个程序计数器，用来记录它下一条指令是什么等等。</p>
</li>
</ul>
<h3 id="1-1-5-方法区"><a href="#1-1-5-方法区" class="headerlink" title="1.1.5 方法区"></a>1.1.5 方法区</h3><p><img src="https://gitee.com/littlefxc/oss/raw/master/images/JVM%E5%86%85%E5%AD%98%E7%BB%93%E6%9E%84-%E6%96%B9%E6%B3%95%E5%8C%BA.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/JVM内存结构-方法区.png"></p>
<p>方法区主要包括 4 个部分：类信息、运行时常量池、字符串常量池和静态变量。</p>
<p>方法区主要存放的是虚拟机加载的类相关的信息。</p>
<p>从上图中可以看到好几种常量池：静态常量池、运行时常量池和字符串常量池。下面来分析一下这 3 种常量池的作用：</p>
<ul>
<li><p>静态常量池</p>
<p> 也叫 class 文件常量池，主要用来存放：</p>
<ul>
<li>字面量：例如，文本字符串、final 修饰的常量</li>
<li>符号引用：例如，类和接口的全限定名、字段的名称和描述符、方法的名称和描述符</li>
</ul>
</li>
<li><p>运行时常量池</p>
<p> 当类加载到内存中后，JVM 就会将静态常量池中的内容存放到运行时的常量池中。运行时常量池里面存储的主要是编译期间生成的字面量、符号引用等等。</p>
</li>
<li><p>字符串常量池</p>
<p> 也可以理解称运行时常量池分出来的一部分，类加载到内存的时候，字符串会存到字符串常量池里面</p>
</li>
</ul>
<p><strong>为什么要用元空间代替持久代？</strong></p>
<ul>
<li><p>一方面是 Orcale 把 Hotspot 虚拟机和 JRockit 虚拟机收购了，而 JRockit 虚拟机压根就没有永久代的概念。于是为了融合Hotspot 虚拟机和 JRockit 虚拟机，干脆就把它去掉了</p>
</li>
<li><p>另一方面是持久代在使用过程中还是很容易发生故障的</p>
<p>相信很多人都遇到过这种异常 <code>java.lang.OutOfMemoryError: PermGen</code>。在之前的版本中，字符串常量池存在于永久代中，在大量使用字符串的情况下，非常容易出现OOM的异常。此外，J<strong>VM加载的class的总数，方法的大小</strong>等都很难确定，因此对永久代大小的指定难以确定。太小的永久代容易导致永久代内存溢出，太大的永久代则容易导致虚拟机内存紧张。</p>
</li>
<li><p>元空间(Metaspace)，不再与堆连续，而是直接存在于本地内存中，也就是机器的内存。理论上机器内存有多大，元空间的野心就有多大。</p>
</li>
</ul>
<p>当然，还有很多更多深层次的原因，可以参考这篇博文<a target="_blank" rel="noopener" href="https://www.cnblogs.com/duanxz/p/3520829.html">Metaspace 之一：Metaspace整体介绍（永久代被替换原因、元空间特点、元空间内存查看分析方法）</a></p>
<p><strong>示例：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JVMTest1</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">    Demo demo = <span class="keyword">new</span> Demo(<span class="string">&quot;aaa&quot;</span>);</span><br><span class="line">    demo.printName();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Demo</span> </span>&#123;</span><br><span class="line">  <span class="keyword">private</span> String name;</span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="title">Demo</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.name = name;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">printName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    System.out.println(<span class="keyword">this</span>.name);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>上面这段代码的内存分布大致是这样的：</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/image-20210223220317252.png" alt="image-20210223220317252"></p>
<ol>
<li><p>在启动的时候首先将类加载到方法区，要加载两个类分别是 JVMTest1.class 和 Demo.class 。</p>
</li>
<li><p>当创建 Demo 对象的时候，首先会创建一个局部变量 demo 放在栈里面并指向到一个引用，而真正的 Demo 对象会存储到堆里面。</p>
</li>
<li><p>最后执行 printName 方法。</p>
</li>
</ol>
<h2 id="1-2-类加载机制"><a href="#1-2-类加载机制" class="headerlink" title="1.2 类加载机制"></a>1.2 类加载机制</h2><p><img src="https://gitee.com/littlefxc/oss/raw/master/images/%E7%B1%BB%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B%E8%AF%A6%E8%A7%A3.png" alt="https://gitee.com/littlefxc/oss/raw/master/images/类加载过程详解.png"></p>
<h3 id="1-2-1-编译"><a href="#1-2-1-编译" class="headerlink" title="1.2.1 编译"></a>1.2.1 编译</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">javac JVMTest1.java</span><br></pre></td></tr></table></figure>

<p>结果产生了两个 class 文件，如下所示：</p>
<p>JVMTest1.class</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">// Source code recreated from a .class file by IntelliJ IDEA</span></span><br><span class="line"><span class="comment">// (powered by Fernflower decompiler)</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">package</span> com.fengxuechao.jvm;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JVMTest1</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">JVMTest1</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] var0)</span> </span>&#123;</span><br><span class="line">        Demo var1 = <span class="keyword">new</span> Demo(<span class="string">&quot;aaa&quot;</span>);</span><br><span class="line">        var1.printName();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Demo.class</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">// Source code recreated from a .class file by IntelliJ IDEA</span></span><br><span class="line"><span class="comment">// (powered by Fernflower decompiler)</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">package</span> com.fengxuechao.jvm;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Demo</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Demo</span><span class="params">(String var1)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.name = var1;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">printName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="keyword">this</span>.name);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>反编译</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">javap -v -p Demo &gt; a.txt</span><br></pre></td></tr></table></figure>

<p>反编译结果如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 描述信息</span><br><span class="line">Classfile &#x2F;Users&#x2F;fengxuechao&#x2F;WorkSpace&#x2F;IdeaProjects&#x2F;foodie&#x2F;jvm&#x2F;src&#x2F;main&#x2F;java&#x2F;com&#x2F;fengxuechao&#x2F;jvm&#x2F;Demo.class</span><br><span class="line">  Last modified 2021-2-24; size 461 bytes</span><br><span class="line">  MD5 checksum 4232b9a7f5bc219b5c632adabd643a3d</span><br><span class="line">  Compiled from &quot;JVMTest1.java&quot;</span><br><span class="line">&#x2F;&#x2F; 描述信息</span><br><span class="line">class com.fengxuechao.jvm.Demo</span><br><span class="line">  minor version: 0</span><br><span class="line">  major version: 52</span><br><span class="line">  flags: ACC_SUPER</span><br><span class="line">&#x2F;&#x2F; 常量池</span><br><span class="line">Constant pool:</span><br><span class="line">   #1 &#x3D; Methodref          #6.#17         &#x2F;&#x2F; java&#x2F;lang&#x2F;Object.&quot;&lt;init&gt;&quot;:()V</span><br><span class="line">   #2 &#x3D; Fieldref           #5.#18         &#x2F;&#x2F; com&#x2F;fengxuechao&#x2F;jvm&#x2F;Demo.name:Ljava&#x2F;lang&#x2F;String;</span><br><span class="line">   #3 &#x3D; Fieldref           #19.#20        &#x2F;&#x2F; java&#x2F;lang&#x2F;System.out:Ljava&#x2F;io&#x2F;PrintStream;</span><br><span class="line">   #4 &#x3D; Methodref          #21.#22        &#x2F;&#x2F; java&#x2F;io&#x2F;PrintStream.println:(Ljava&#x2F;lang&#x2F;String;)V</span><br><span class="line">   #5 &#x3D; Class              #23            &#x2F;&#x2F; com&#x2F;fengxuechao&#x2F;jvm&#x2F;Demo</span><br><span class="line">   #6 &#x3D; Class              #24            &#x2F;&#x2F; java&#x2F;lang&#x2F;Object</span><br><span class="line">   #7 &#x3D; Utf8               name</span><br><span class="line">   #8 &#x3D; Utf8               Ljava&#x2F;lang&#x2F;String;</span><br><span class="line">   #9 &#x3D; Utf8               &lt;init&gt;</span><br><span class="line">  #10 &#x3D; Utf8               (Ljava&#x2F;lang&#x2F;String;)V</span><br><span class="line">  #11 &#x3D; Utf8               Code</span><br><span class="line">  #12 &#x3D; Utf8               LineNumberTable</span><br><span class="line">  #13 &#x3D; Utf8               printName</span><br><span class="line">  #14 &#x3D; Utf8               ()V</span><br><span class="line">  #15 &#x3D; Utf8               SourceFile</span><br><span class="line">  #16 &#x3D; Utf8               JVMTest1.java</span><br><span class="line">  #17 &#x3D; NameAndType        #9:#14         &#x2F;&#x2F; &quot;&lt;init&gt;&quot;:()V</span><br><span class="line">  #18 &#x3D; NameAndType        #7:#8          &#x2F;&#x2F; name:Ljava&#x2F;lang&#x2F;String;</span><br><span class="line">  #19 &#x3D; Class              #25            &#x2F;&#x2F; java&#x2F;lang&#x2F;System</span><br><span class="line">  #20 &#x3D; NameAndType        #26:#27        &#x2F;&#x2F; out:Ljava&#x2F;io&#x2F;PrintStream;</span><br><span class="line">  #21 &#x3D; Class              #28            &#x2F;&#x2F; java&#x2F;io&#x2F;PrintStream</span><br><span class="line">  #22 &#x3D; NameAndType        #29:#10        &#x2F;&#x2F; println:(Ljava&#x2F;lang&#x2F;String;)V</span><br><span class="line">  #23 &#x3D; Utf8               com&#x2F;fengxuechao&#x2F;jvm&#x2F;Demo</span><br><span class="line">  #24 &#x3D; Utf8               java&#x2F;lang&#x2F;Object</span><br><span class="line">  #25 &#x3D; Utf8               java&#x2F;lang&#x2F;System</span><br><span class="line">  #26 &#x3D; Utf8               out</span><br><span class="line">  #27 &#x3D; Utf8               Ljava&#x2F;io&#x2F;PrintStream;</span><br><span class="line">  #28 &#x3D; Utf8               java&#x2F;io&#x2F;PrintStream</span><br><span class="line">  #29 &#x3D; Utf8               println</span><br><span class="line">&#x2F;&#x2F; 字段信息</span><br><span class="line">&#123;</span><br><span class="line">  private java.lang.String name;</span><br><span class="line">    descriptor: Ljava&#x2F;lang&#x2F;String;</span><br><span class="line">    flags: ACC_PRIVATE</span><br><span class="line">&#x2F;&#x2F; 方法的信息</span><br><span class="line">  public com.fengxuechao.jvm.Demo(java.lang.String);</span><br><span class="line">    descriptor: (Ljava&#x2F;lang&#x2F;String;)V</span><br><span class="line">    flags: ACC_PUBLIC</span><br><span class="line">    Code:</span><br><span class="line">      stack&#x3D;2, locals&#x3D;2, args_size&#x3D;2</span><br><span class="line">         0: aload_0</span><br><span class="line">         1: invokespecial #1                  &#x2F;&#x2F; Method java&#x2F;lang&#x2F;Object.&quot;&lt;init&gt;&quot;:()V</span><br><span class="line">         4: aload_0</span><br><span class="line">         5: aload_1</span><br><span class="line">         6: putfield      #2                  &#x2F;&#x2F; Field name:Ljava&#x2F;lang&#x2F;String;</span><br><span class="line">         9: return</span><br><span class="line">      LineNumberTable:</span><br><span class="line">        line 16: 0</span><br><span class="line">        line 17: 4</span><br><span class="line">        line 18: 9</span><br><span class="line"></span><br><span class="line">  public void printName();</span><br><span class="line">    descriptor: ()V</span><br><span class="line">    flags: ACC_PUBLIC</span><br><span class="line">    Code:</span><br><span class="line">      stack&#x3D;2, locals&#x3D;1, args_size&#x3D;1</span><br><span class="line">         0: getstatic     #3                  &#x2F;&#x2F; Field java&#x2F;lang&#x2F;System.out:Ljava&#x2F;io&#x2F;PrintStream;</span><br><span class="line">         3: aload_0</span><br><span class="line">         4: getfield      #2                  &#x2F;&#x2F; Field name:Ljava&#x2F;lang&#x2F;String;</span><br><span class="line">         7: invokevirtual #4                  &#x2F;&#x2F; Method java&#x2F;io&#x2F;PrintStream.println:(Ljava&#x2F;lang&#x2F;String;)V</span><br><span class="line">        10: return</span><br><span class="line">      LineNumberTable:</span><br><span class="line">        line 21: 0</span><br><span class="line">        line 22: 10</span><br><span class="line">&#125;</span><br><span class="line">SourceFile: &quot;JVMTest1.java&quot;</span><br></pre></td></tr></table></figure>

<p>指令参考：<a target="_blank" rel="noopener" href="https://docs.oracle.com/javase/specs/jvms/se8/html/index.html">https://docs.oracle.com/javase/specs/jvms/se8/html/index.html</a></p>
<h3 id="1-2-2-加载"><a href="#1-2-2-加载" class="headerlink" title="1.2.2 加载"></a>1.2.2 加载</h3><ul>
<li><p>JVM 是如何加载 class 文件的呢？</p>
<p>当一个类被创建实例或者被引用到的时候, 如果虚拟机发现之前没有加载过这个类，就会通过类加载器（也就是 ClassLoader ）把 class 文件加载到内存。</p>
<p>在加载的过程中主要做了 3 件事：</p>
<ol>
<li>读取类的二进制流。</li>
<li>把二进制流转为方法区数据结构，并存放到方法区。</li>
<li>最后，在 Java 堆中产生 java.lang.Class 对象。</li>
</ol>
<p>文档参考：<a target="_blank" rel="noopener" href="https://www.cnblogs.com/williamjie/p/11167920.html">描述一下JVM加载class文件的原理机制</a></p>
</li>
</ul>
<h3 id="1-2-3-链接"><a href="#1-2-3-链接" class="headerlink" title="1.2.3 链接"></a>1.2.3 链接</h3><p>class 文件加载完成后，会进入“链接”这个步骤，链接这个步骤又可以分为“验证”、“准备”和“解析”。</p>
<h4 id="1-2-3-1-验证"><a href="#1-2-3-1-验证" class="headerlink" title="1.2.3.1 验证"></a>1.2.3.1 验证</h4><p>验证——顾名思义，就是验证 class 文件是不是符合规范，这里面包含了多个层次的验证，包括：</p>
<ul>
<li><p>文件格式的验证</p>
<ul>
<li>是否以 <code>0xCAFEBABE</code> 开头（可以用 16 进制编辑器打开查看）</li>
<li>版本好是否合理</li>
</ul>
</li>
<li><p>元数据验证</p>
<ul>
<li>是否有父类</li>
<li>是否继承了 final 类（final 类是不能被继承的，如果继承了，那就是有问题）</li>
<li>非抽象类是否实现了所有抽象方法（没有，那就是有问题）</li>
</ul>
</li>
<li><p>字节码验证</p>
<p>字节码的验证是非常复杂的，一个 class 文件能够通过字节码验证并不代表它没有问题，但是如果它没有通过字节码验证，那就一定有问题。</p>
<ul>
<li>运行检查</li>
<li>栈数据类型和操作码操作参数吻合（比如栈空间只有 2 字节，但其实却需要大于 2 字节，此时就认为这个字节码是有问题的）</li>
<li>跳转指令是不是指向合理的位置</li>
</ul>
</li>
<li><p>符号引用验证</p>
<ul>
<li>常量池中描述类是否存在</li>
<li>访问的方法或字段是否存在且有足够的权限</li>
</ul>
</li>
<li><p>如果事先已经确认代码是安全无误的可以在启动的时候用<code>-Xverify:none</code>关闭验证。</p>
</li>
</ul>
<h4 id="1-2-3-2-准备"><a href="#1-2-3-2-准备" class="headerlink" title="1.2.3.2 准备"></a>1.2.3.2 准备</h4><p>如果通过验证发现没有问题的话，就会进入准备环节。</p>
<p>准备环节的作用：</p>
<ul>
<li>为类的静态变量分配内存，初始化为系统的初始值<ul>
<li>final static 修饰的变量：直接赋值为用户定义的值，比如 <code>private final static int value =123</code>，直接赋值 123。</li>
<li>但是 <code>private static int value = 123</code>, 该阶段的值依然是 0。</li>
</ul>
</li>
</ul>
<h4 id="1-2-3-3-解析"><a href="#1-2-3-3-解析" class="headerlink" title="1.2.3.3 解析"></a>1.2.3.3 解析</h4><p>“准备”完成后，就可以进入解析了。</p>
<p>解析的作用：把符号引用转换成直接引用。</p>
<ul>
<li><p>什么是符号引用？</p>
<p>符号引用（Symbolic References）：符号引用以一组符号来描述所引用的目标，符号可以是任何形式的字面量，只要使用时能够无歧义的定位到目标即可。例如，在Class文件中它以CONSTANT_Class_info、CONSTANT_Fieldref_info、CONSTANT_Methodref_info等类型的常量出现。符号引用与虚拟机的内存布局无关，引用的目标并不一定加载到内存中。在Java中，一个java类将会编译成一个class文件。在编译时，java类并不知道所引用的类的实际地址，因此只能使用符号引用来代替。比如org.simple.People类引用了org.simple.Language类，在编译时People类并不知道Language类的实际内存地址，因此只能使用符号org.simple.Language（假设是这个，当然实际中是由类似于CONSTANT_Class_info的常量来表示的）来表示Language类的地址。各种虚拟机实现的内存布局可能有所不同，但是它们能接受的符号引用都是一致的，因为符号引用的字面量形式明确定义在Java虚拟机规范的Class文件格式中。</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/image-20210224212914755.png" alt="image-20210224212914755"></p>
</li>
<li><p>什么是直接引用？</p>
<p>可以是</p>
<ul>
<li>直接指向目标的指针（比如，指向“类型”【Class对象】、类变量、类方法的直接引用可能是指向方法区的指针）</li>
<li>相对偏移量（比如，指向实例变量、实例方法的直接引用都是偏移量）</li>
<li>一个能间接定位到目标的句柄</li>
</ul>
<p>直接引用是和虚拟机的布局相关的，同一个符号引用在不同的虚拟机实例上翻译出来的直接引用一般不会相同。如果有了直接引用，那引用的目标必定已经被加载入内存中了。</p>
</li>
</ul>
<h4 id="1-2-4-初始化"><a href="#1-2-4-初始化" class="headerlink" title="1.2.4 初始化"></a>1.2.4 初始化</h4><p>解析完成之后，就会进入初始化这个阶段。在这个阶段，JVM首先会执行 <clinit> 方法，clinit 方法由编译器自动收集里面的所有静态变量赋值动作和静态语句块合并而成，也叫类构造器方法</p>
<ul>
<li>初始化的顺序和源文件中的顺序一致</li>
<li>子类的 <clinit> 被调用前，会先调用父类的 <clinit></li>
<li>JVM 会保证 clinit 方法的线程安全性</li>
</ul>
<p>示例1：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">public class JVMTest2 &#123;</span><br><span class="line"></span><br><span class="line">    static &#123;</span><br><span class="line">        System.out.println(&quot;JVMTest2 静态块&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    &#123;</span><br><span class="line">        System.out.println(&quot;JVMTest2 构造块&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public JVMTest2() &#123;</span><br><span class="line">        System.out.println(&quot;JVMTest2 构造方法&quot;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        System.out.println(&quot;JVMTest2 main() 方法&quot;);</span><br><span class="line">        new JVMTest2();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>执行结果：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">JVMTest2 静态块</span><br><span class="line">JVMTest2 main() 方法</span><br><span class="line">JVMTest2 构造块</span><br><span class="line">JVMTest2 构造方法</span><br></pre></td></tr></table></figure>

<p>示例2:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JVMTest3</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">static</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;JVMTest3 静态块&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;JVMTest3 构造块&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">JVMTest3</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;JVMTest3 构造方法&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;JVMTest3 main() 方法&quot;</span>);</span><br><span class="line">        <span class="keyword">new</span> Sub();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Super</span> </span>&#123;</span><br><span class="line">    <span class="keyword">static</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Super 静态块&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Super 构造块&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Super</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Super 构造方法&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Sub</span> <span class="keyword">extends</span> <span class="title">Super</span> </span>&#123;</span><br><span class="line">    <span class="keyword">static</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Sub 静态块&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    &#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Sub 构造块&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Sub</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;Sub 构造方法&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>执行结果：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">JVMTest3 静态块</span><br><span class="line">JVMTest3 main() 方法</span><br><span class="line">Super 静态块</span><br><span class="line">Sub 静态块</span><br><span class="line">Super 构造块</span><br><span class="line">Super 构造方法</span><br><span class="line">Sub 构造块</span><br><span class="line">Sub 构造方法</span><br></pre></td></tr></table></figure>

<h3 id="1-2-5-使用"><a href="#1-2-5-使用" class="headerlink" title="1.2.5 使用"></a>1.2.5 使用</h3><p>初始化完成之后就可以使用这个类了。</p>
<h3 id="1-2-6-卸载"><a href="#1-2-6-卸载" class="headerlink" title="1.2.6 卸载"></a>1.2.6 卸载</h3><p>但不使用这个类的话，可以把它卸载掉。</p>
<h3 id="1-2-7-小节"><a href="#1-2-7-小节" class="headerlink" title="1.2.7 小节"></a>1.2.7 小节</h3><p>章节 1.2 的图表示的是一般的类加载流程，而事实上类加载的时候并不一定完全按照这个流程走。例如，解析不一定在初始化之前，也有可能在初始化之后去做。</p>
<h2 id="1-3-编译器优化"><a href="#1-3-编译器优化" class="headerlink" title="1.3 编译器优化"></a>1.3 编译器优化</h2><h2 id="1-4-垃圾收集算法"><a href="#1-4-垃圾收集算法" class="headerlink" title="1.4 垃圾收集算法"></a>1.4 垃圾收集算法</h2><h2 id="1-5-垃圾收集器"><a href="#1-5-垃圾收集器" class="headerlink" title="1.5 垃圾收集器"></a>1.5 垃圾收集器</h2><h1 id="2-工具"><a href="#2-工具" class="headerlink" title="2 工具"></a>2 工具</h1><h1 id="3-实战"><a href="#3-实战" class="headerlink" title="3 实战"></a>3 实战</h1><h1 id="4-参考资源"><a href="#4-参考资源" class="headerlink" title="4 参考资源"></a>4 参考资源</h1><p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/andy-zhou/p/5327288.html">JVM调优总结</a></p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/blog/tags/JVM/" rel="tag"># JVM</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/blog/2021/02/19/KAFKA-%E9%85%8D%E7%BD%AE%E5%8F%82%E6%95%B0%E8%AF%A6%E8%A7%A3/" rel="prev" title="KAFKA 配置参数详解">
                  <i class="fa fa-chevron-left"></i> KAFKA 配置参数详解
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/blog/2021/02/24/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95/" rel="next" title="设计模式学习记录">
                  设计模式学习记录 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">一年春又来</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/mist/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/blog/js/utils.js"></script><script src="/blog/js/motion.js"></script><script src="/blog/js/schemes/muse.js"></script><script src="/blog/js/next-boot.js"></script>

  
<script src="/blog/js/local-search.js"></script>






  





</body>
</html>
